//
//  BIDMeetingRemindDetailViewController.m
//  cxgov
//
//  Created by mal on 16/3/15.
//  Copyright © 2016年 mal. All rights reserved.
//

#import "BIDMeetingRemindDetailViewController.h"
#import "BIDMenuView.h"
#import "BIDReplyView.h"
#import "BIDCreateMeetingRemindViewController.h"
#import "BIDReceiveConditionForRemindViewController.h"

//已收详情
static NSString *receivedDetailURL = @"meetingAndroid!toReadMeeting.action";
//已发详情
static NSString *sentDetailURL = @"meetingAndroid!toReadSendMeeting.action";
//回复
static NSString *replyURL = @"noticeAndroid!addNotice.action";
//已收提醒删除
static NSString *deleteReceivedRemindURL = @"meetingAndroid!dropMeetingReply.action";
//已发提醒删除
static NSString *deleteSentRemindURL = @"meetingAndroid!dropSendMeeting.action";

@interface BIDMeetingRemindDetailViewController ()<BIDMenuViewDelegate, BIDReplyViewDelegate>
{
    BIDMenuView *_menuView;
    BIDReplyView *_replyView;
    BOOL _bShowMenu;
}
@end

@implementation BIDMeetingRemindDetailViewController
@synthesize bReceived;
@synthesize remindId;
@synthesize creatorId;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详情";
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_more.png"] style:UIBarButtonItemStylePlain target:self action:@selector(functionHandler)];
    //
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    _replyView = [[BIDReplyView alloc] initWithFrame:CGRectMake(0, -screenSize.height, 0, 0)];
    _replyView.titleLabel.text = @"写回复";
    _replyView.delegate = self;
    //
    int fontSize = [AppDelegate getFontSize];
    _contentLabel.font = [UIFont systemFontOfSize:fontSize];
    //
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadData
{
    NSString *strDesURL = bReceived?receivedDetailURL:sentDetailURL;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], strDesURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"params=%@", remindId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(NSDictionary *responseDic, NSString *errMsg){
        [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
            if(responseObj)
            {
                NSDictionary *responseDic = (NSDictionary*)responseObj;
                NSString *strTitle = responseDic[@"title"];
                NSString *strTime = responseDic[@"meetingTime"];
                NSString *strPlace = responseDic[@"address"];
                NSString *strContent = responseDic[@"content"];
                _titleLabel.text = strTitle;
                _timeLabel.text = strTime;
                _placeLabel.text = strPlace;
                _contentLabel.text = strContent;
                //
                CGFloat titleHeight = [BIDGlobalMethods getHeightWithString:strTitle font:_titleLabel.font constraintSize:CGSizeMake(CGRectGetWidth(self.view.frame)-8*2, MAXFLOAT)];
                _heightForTitleConstraint.constant = titleHeight;
                [self.view layoutIfNeeded];
                //
                CGFloat timeWidth = [BIDGlobalMethods getWidthWithString:strTime font:_timeLabel.font constraintSize:CGSizeMake(MAXFLOAT, CGRectGetHeight(_timeLabel.frame))];
                _widthForTimeConstraint.constant = timeWidth;
                [self.view layoutIfNeeded];
                //
                CGFloat placeWidth = [BIDGlobalMethods getWidthWithString:strPlace font:_placeLabel.font constraintSize:CGSizeMake(MAXFLOAT, CGRectGetHeight(_placeLabel.frame))];
                _widthForPlaceConstraint.constant = placeWidth;
                [self.view layoutIfNeeded];
                //
                CGFloat contentHeight = [BIDGlobalMethods getHeightWithString:strContent font:_contentLabel.font constraintSize:CGSizeMake(CGRectGetWidth(self.view.frame)-8*2, MAXFLOAT)];
                _heightForContentConstraint.constant = contentHeight;
                [self.view layoutIfNeeded];
                //
                _heightForContainerViewConstraint.constant = CGRectGetMinY(_contentLabel.frame)+contentHeight+20.f;
                [self.view layoutIfNeeded];
            }
        }];
    }];
}

/**
 *  更多
 */
- (void)functionHandler
{
    //弹出菜单
    if(!_menuView)
    {
        if(bReceived)
        {
            NSArray *menuNameArr = @[@"快捷回复", @" 回复        ", @" 转发        ", @" 删除        "];
            CGSize screenSize = [UIScreen mainScreen].bounds.size;
            //menuWidth,menuHeight,triangleWidth,triangleHeight,triangleToRight
            BIDMenuAttribute menuAttribute = {120, 5, 10, 15, 14};
            int menuViewPosX = screenSize.width - 26 - (menuAttribute.menuWidth-menuAttribute.triangleToRight-menuAttribute.triangleWidth/2);
            int menuViewPosY = 2;
            //NSArray *imgNameArr = @[@"menu_fast_reply.png", @"menu_reply.png", @"menu_forwarding.png", @"menu_delete.png"];
            _menuView = [[BIDMenuView alloc] initWithFrame:CGRectMake(menuViewPosX, menuViewPosY, menuAttribute.menuWidth, menuAttribute.menuHeight) menuNameArr:menuNameArr imgNameArr:nil menuAttribute:menuAttribute];
            _menuView.delegate = self;
        }
        else
        {
            NSArray *menuNameArr = @[@" 接收情况", @" 转发        ", @" 删除        "];
            CGSize screenSize = [UIScreen mainScreen].bounds.size;
            //menuWidth,menuHeight,triangleWidth,triangleHeight,triangleToRight
            BIDMenuAttribute menuAttribute = {120, 5, 10, 15, 14};
            int menuViewPosX = screenSize.width - 26 - (menuAttribute.menuWidth-menuAttribute.triangleToRight-menuAttribute.triangleWidth/2);
            int menuViewPosY = 2;
            NSArray *imgNameArr = @[@"menu_receive_condition.png", @"menu_forwarding.png", @"menu_delete.png"];
            _menuView = [[BIDMenuView alloc] initWithFrame:CGRectMake(menuViewPosX, menuViewPosY, menuAttribute.menuWidth, menuAttribute.menuHeight) menuNameArr:menuNameArr imgNameArr:imgNameArr menuAttribute:menuAttribute];
            _menuView.delegate = self;
        }

    }
    if(!_bShowMenu)
    {
        _bShowMenu = YES;
        [self.view addSubview:_menuView];
    }
    else
    {
        _bShowMenu = NO;
        [UIView animateWithDuration:1.0f animations:^{
            //_menuView.transform = CGAffineTransformMakeScale(0.5f, 0.5f);
        } completion:^(BOOL bFinished){
            if(bFinished)
            {
                [_menuView removeFromSuperview];
            }
        }];
    }
}

//回复提醒
- (void)replyRemindWithContent:(NSString*)replyContent
{
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], replyURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"notice.createUser=%@&receiveId=%@&notice.content=%@", [AppDelegate getUserId], creatorId, replyContent];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            NSNumber *value = responseDic[@"isSuccess"];
            if([value intValue]==1)
            {
                [BIDGlobalMethods showAlertView:@"回复成功" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"回复失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}
//删除列表中的一条提醒

- (void)deleteRemind
{
    NSString *strDesURL = bReceived?deleteReceivedRemindURL:deleteSentRemindURL;
    NSString *strRequestURL = [[NSString alloc] initWithFormat:@"%@/%@", [AppDelegate getServerAddr], strDesURL];
    NSString *strPost = [[NSString alloc] initWithFormat:@"ids=%@", remindId];
    [self sendHttpRequestWithURL:strRequestURL postData:strPost withCompletion:^(id responseObj, NSString *errMsg){
        if(responseObj)
        {
            NSDictionary *responseDic = (NSDictionary*)responseObj;
            NSNumber *value = responseDic[@"isSuccess"];
            if([value intValue]==1)
            {
                [BIDGlobalMethods showAlertView:@"删除成功" buttonTitle:ALERT_BTN_TITLE delegate:self tag:1];
            }
            else
            {
                [BIDGlobalMethods showAlertView:@"删除失败" buttonTitle:ALERT_BTN_TITLE delegate:nil tag:0];
            }
        }
    }];
}

#pragma mark - BIDReplyViewDelegate
- (void)replyWithContent:(NSString *)replyContent
{
    [self replyRemindWithContent:replyContent];
}

#pragma mark - BIDMenuViewDelegate
- (void)selectMenuAtIndex:(int)index
{
    _bShowMenu = NO;
    if(bReceived)
    {
        switch(index)
        {
            case 0:
            {
                //快捷回复
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您确定回复此提醒\"确认收到\"吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                alertView.tag = 2;
                [alertView show];
            }
                break;
            case 1:
            {
                //回复
                [_replyView showTheView];
            }
                break;
            case 2:
            {
                //转发
                BIDCreateMeetingRemindViewController *vc = [[BIDCreateMeetingRemindViewController alloc] initWithNibName:@"BIDCreateMeetingRemindViewController" bundle:nil];
                vc.meetingPlace = _placeLabel.text;
                vc.meetingContent = _contentLabel.text;
                vc.meetingSubject = _titleLabel.text;
                vc.meetingTime = _timeLabel.text;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 3:
            {
                //删除
                [self deleteRemind];
            }
                break;
        }
    }
    else
    {
        switch(index)
        {
            case 0:
            {
                //接收情况
                BIDReceiveConditionForRemindViewController *vc = [[BIDReceiveConditionForRemindViewController alloc] init];
                vc.remindId = remindId;
                vc.bIsMeetingRemind = YES;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 1:
            {
                //转发
                BIDCreateMeetingRemindViewController *vc = [[BIDCreateMeetingRemindViewController alloc] initWithNibName:@"BIDCreateMeetingRemindViewController" bundle:nil];
                vc.meetingPlace = _placeLabel.text;
                vc.meetingContent = _contentLabel.text;
                vc.meetingSubject = _titleLabel.text;
                vc.meetingTime = _timeLabel.text;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 2:
            {
                //删除
                [self deleteRemind];
            }
                break;
        }
    }

}

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 2)
    {
        if(buttonIndex == 1)
        {
            //快捷回复
            [self replyRemindWithContent:@"确认收到"];
        }
    }
    else if(alertView.tag == 1)
    {
        //删除成功
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
